package com.example.travelservlet.managementLogin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.travelservlet.entity.Manager;
import com.example.travelservlet.entity.TotalRaw;
import com.example.travelservlet.managementLogin.service.LoginService;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.utils.LayuiResponse;
import com.example.travelservlet.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kiki
 * @dtae 2023/5/30 8:11
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/loginCheck")
    public Result loginCheck(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("name");
        String password = request.getParameter("pwd");
        //判断用户名密码是否正确，判断身份，设置登录状态
        Manager manager = loginService.findManagerByUsernameAndPassword(username, password);
        if (manager != null) {
            manager.setState(1);
            loginService.setManagerState(manager.getId());
            return ResultUtil.success(manager);
        } else {
            return ResultUtil.error(0);
        }
    }

    @PostMapping("/pageCheck")
    public Result pageCheck(HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("id");
        //根据用户id判断登录状态
        Manager manager = loginService.findManagerById(userid);
        if (manager != null ) {
            int state = manager.getState();
            String role = manager.getRole();
            if (state == 1 && role.equals("super")) {
                return ResultUtil.success(1);
            } else if(state == 1 && role.equals("normal")) {
                return ResultUtil.success(2);
            } else {
                return ResultUtil.error(0);
            }
        } else {
            return ResultUtil.error(2);
        }
    }

    @GetMapping("/signOut")
    public Result signOut(HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("id");
        //根据用户id退出登录
        Integer out = loginService.signout(userid);
        if (out == 1) {
            return ResultUtil.success(1);
        } else {
            return ResultUtil.error(0);
        }
    }

    @PostMapping("/editInfoById")
    public Result editInfo(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String username = request.getParameter("name");
        String password = request.getParameter("pwd");
        if (username == "" || password == "") {
            return ResultUtil.error(1);
        } else {
            Boolean result = loginService.editManagerById(id, username, password);
            if (result == true) {
                return ResultUtil.success(1);
            } else {
                return ResultUtil.error(0);
            }
        }
    }

    @GetMapping("/searchInfoById")
    public Result searchInfoById(HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("id");
        Manager manager = loginService.findManagerById(userid);
        if (manager != null) {
            return ResultUtil.success(manager);
        } else {
            return ResultUtil.error(0);
        }
    }

    @GetMapping("/deleteById")
    public Result deleteById(HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("id");
        loginService.deleteById(userid);
        return ResultUtil.success(1);
    }

    @PostMapping("/addManager")
    public Result addManager(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("name");
        String password = request.getParameter("pwd");
        if (username == "" || password == "") {
            return ResultUtil.error(1);
        } else {
            Boolean result = loginService.addManger(username, password);
            if (result == true) {
                return ResultUtil.success(1);
            } else {
                return ResultUtil.error(0);
            }
        }
    }

    @GetMapping("/findAllManager")
    public LayuiResponse findAllManager(HttpServletRequest request, HttpServletResponse response) {
        List<Manager> managerList = loginService.findAllManager();
        if (managerList != null) {
            TotalRaw totalRaw = new TotalRaw();
            totalRaw.setCheckIn("777");
            return LayuiResponse.ok(managerList, null, null);
        } else {
            return null;
        }
    }

}
